﻿using OpenCvSharpExtension;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPF.DEMO
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly;

            this.Loaded += MainWindow_Loaded;
            //var a = Vector.AngleBetween(new Vector(-1, 0), new Vector(1, 1));
            ////var c=new Vector(-1,0)+new Vector(1,1);

            var vv = VectorHelper.Projection(new Vector(100, 2), new Vector(200, 1));
            var s = Math.Sin(Math.PI / 6);
            var g = Math.Pow(2, 0);

            ulong uv1 = BitConverter.DoubleToUInt64Bits(g);

            byte[] buf = BitConverter.GetBytes(g);
            ulong uv2 = BitConverter.ToUInt64(buf, 0);
            var f = (float)int.MaxValue;
            EllipseGeometry eg = new EllipseGeometry();


            //SharpSoft.WPF.ItemsControlDragExtension.AttachDrag(ic);

            //ic.ItemsSource = new string[] { "张三", "李四", "王五" };
            //ic.ItemsSource = new List<string> { "张三", "李四", "王五" };
            //ic.MouseLeftButtonDown += Ic_MouseLeftButtonDown;
            //ic.MouseLeftButtonUp += Ic_MouseLeftButtonUp;
            //ic.DragOver += Ic_DragOver;
        }

        private void Ic_DragOver(object sender, DragEventArgs e)
        {
        }

        private void Ic_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //var p = e.GetPosition(ic);
            //var item = ic.InputHitTest(p);
            //var c = SharpSoft.WPF.VisualHelper.FindParent<ContentPresenter>((DependencyObject)item);//找到点击项的父级ContentPresenter
            //if (c != null)
            //{
            //    c.Visibility = Visibility.Visible;
            //    var s = ic.ItemContainerGenerator.ItemFromContainer(c);//找到点击的项
            //}
        }

        private async void Ic_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // (menu.Children[menu.Children.Count - 1] as RadioButton).IsChecked = true;
            foreach (var item in menu.Children)
            {
                if (item is RadioButton rb && rb.Content is string str && str == "序列帧")
                {
                    rb.IsChecked = true;
                    break;
                }

            }
            //var decoder = System.Windows.Media.Imaging.BitmapDecoder.Create(new Uri(@"C:\Users\sharp\Pictures\未标题-2.png"), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
            //var frame = decoder.Frames[0];
            //var bs = SharpSoft.WPF.BitmapHelper.ToByteArray(frame);
            //var s = $"{bs.Length / (1024d * 1024):0.00}";
            //WriteableBitmap bmp = new WriteableBitmap(frame.PixelWidth, frame.PixelHeight, frame.DpiX, frame.DpiY, frame.Format, frame.Palette);
            //SharpSoft.WPF.BitmapHelper.Blit(bmp, bs);
            //// img.Source = bmp;

            //SharpSoft.WPF.Controls.SequenceImage.GenerateSequenceImageFile(@"C:\Users\sharp\Documents\Tencent Files\534994635\FileRecv\邮政动画\邮政男", @"res\a.bin", 24, 0);

            //var fs = new FileStream(@"res\a.bin", FileMode.Open, FileAccess.Read);
            //var info = SharpSoft.WPF.Controls.SequenceImage.OpenSequenceImageFile(fs);
            //var pf = info.GetPixelFormat();

            this.Background = new ImageBrush(BitmapHelper.LoadImageByOpenCV(@"res\webpTest.webp"));


            //SaveAsImages(@" sda", this.MapPath("aaaa"));
        }

        private void StackPanel_Checked(object sender, RoutedEventArgs e)
        {
            if (e.Source is RadioButton rb && rb.Tag is Type t)
            {
                var obj = Activator.CreateInstance(t);
                if (obj is Page p)
                {
                    frame.Navigate(p);
                }
            }
        }

        private void frame_Navigated(object sender, NavigationEventArgs e)
        {

        }

        private void frame_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            tec.SnapshotAndStart();
        }

        private void SaveAsImages(string file, string folder)
        {
            var Renderer = new PdfiumViewer.PdfRenderer();
            var bytes = File.ReadAllBytes(file);
            var mem = new MemoryStream(bytes);
            Renderer.OpenPdf(mem);
            if (!System.IO.Directory.Exists(folder))
            {
                System.IO.Directory.CreateDirectory(folder);
            }
            try
            {
                System.Drawing.Image img1 = null, img2;
                int lastwidth = 0;
                for (var i = 0; i < Renderer.PageCount; i++)
                {
                    var size = Renderer.Document.PageSizes[i];
                    var image = Renderer.Document.Render(i, (int)(size.Width * 2f), (int)(size.Height * 2f), 300, 300, false);
                    // image.Save(System.IO.Path.Combine(folder, $"img{i}.png"));
                    if (i % 2 == 0)
                    {
                        img1 = image;
                    }
                    else
                    {
                        img2 = image;

                        System.Drawing.Image img = new System.Drawing.Bitmap(img1.Width + img2.Width, Math.Max(img1.Height, img2.Height));
                        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img))
                        {
                            g.DrawImage(img1, 0, 0, img1.Width, img1.Height);
                            g.DrawImage(img2, img1.Width, 0, img2.Width, img2.Height);
                            g.Flush();
                        }
                        lastwidth = img1.Width + img2.Width;
                        img1 = img2 = null;
                        img.Save(System.IO.Path.Combine(folder, $"{i:0000}.jpg"));
                    }
                }
                if (img1 != null)
                {
                    System.Drawing.Image img = new System.Drawing.Bitmap(lastwidth, img1.Height);
                    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img))
                    {
                        g.DrawImage(img1, 0, 0, img1.Width, img1.Height);
                        g.Flush();
                    }
                    lastwidth = img1.Width;
                    img.Save(System.IO.Path.Combine(folder, $"{Renderer.PageCount:0000}.jpg"));
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private void Window_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
